//我的页面
const util = require("../../utils/util.js");
import store from "../../utils/factory";
import {
  logout,
  getShopCarList,
  getStatistics,
  addVip,
  getWallet
} from "../../utils/config";

let that = null;
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === "function" && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3,
          dialog1: false
        });
        this.getTabBar().setData({
          count: 0
        });
        if (store.get("userInfo")) {
          this.getTotal();
        }
      }
      if (store.get("userInfo")) {
        if (store.get("userInfo").vip) {
          this.getWallet();
        }
        this.getStatistics();
        this.setData({
          phone: store.get("userInfo").phone,
          vip: store.get("userInfo").vip
        });
      }
    }
  },
  data: {
    phone: "",
    needPayCount: 0,
    needSendCount: 0,
    needReceivedCount: 0,
    refundCount: 0,
    dialog1: false,
    vip: false,
    ponit: 0,
    money: 0,
    invite: ""
  }, // 私有数据，可用于模板渲染
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () {
    that = this;
  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () {},
  methods: {
    onShareAppMessage() {
      return {
        title: "热度零食铺",
        path: '/pages/index/index'
      }
    },
    getWallet() {
      getWallet().then(res => {
        const data = res.data;
        this.setData({
          money: data.wallet,
          point: data.point,
          invite: data.createUser.invite
        })
      })
    },
    logout() {
      wx.showModal({
        title: "温馨提示",
        content: "确认退出登录?",
        success: function (sm) {
          if (sm.confirm) {
            logout().then(res => {
              store.clear();
              that.setData({
                phone: ""
              });
              that.getTabBar().setData({
                count: 0
              });
            });
          } else if (sm.cancel) {
            console.log("用户点击取消");
          }
        }
      });
    },
    close() {
      this.setData({
        dialog1: false
      })
    },
    kaitong() {
      addVip().then(res => {
        let userInfo = store.get("userInfo");
        userInfo.vip = true;
        store.set("userInfo", userInfo)
        this.setData({
          vip: true,
          dialog1: false
        })
      })
    },
    seeVip() {
      wx.showToast({
        title: '复制成功',
      })
      wx.setClipboardData({
        data: that.data.invite,
        success: function (res) {
          wx.getClipboardData({
            success: function (res) {
              console.log(res.data) // data
            }
          })
        }
      })
    },
    openVip() {
      this.setData({
        dialog1: true
      })
    },
    getTotal() {
      getShopCarList().then(res => {
        let req = res.data;
        this.getTabBar().setData({
          count: req.quantity || 0
        });
      });
    },
    getStatistics() {
      getStatistics().then(res => {
        let req = res.data;
        this.setData({
          needPayCount: req.needPay,
          needReceivedCount: req.needReceived,
          needSendCount: req.needSend,
          refundCount: req.refund
        });
      });
    },
    goPage(e) {
      if (!store.get("userInfo")) {
        wx.navigateTo({
          url: "../autoLogin/autoLogin" // 页面 B
        });
        return false;
      }
      let url = e.currentTarget.dataset.url;
      if (url.indexOf("wallet") > 0) {
        if (!this.data.vip) {
          wx.showModal({
            title: "温馨提示",
            content: "请先开通热度零食铺会员",
            showCancel: false, //是否显示取消按钮
            success: function (res) {
              that.openVip()
            }
          });
          return false
        }
      }
      wx.navigateTo({
        url: url
      });
    },
    login() {
      wx.navigateTo({
        url: "../autoLogin/autoLogin"
      });
    },

  }
});